package top.drinyeit.nongmao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;

import com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.adapter.MessageAdapter;
import top.drinyeit.nongmao.base.BaseActivity;
import top.drinyeit.nongmao.model.MessageBean;
import top.drinyeit.nongmao.presenter.MessagePresenter;

/**
 * Created by bigHead on 2017/11/20.
 */

public class MessageActivity extends BaseActivity {

    private RecyclerView recyclerView;
    public SHSwipeRefreshLayout shSwipeRefreshLayout;
    private List<MessageBean> list;
    private MessageAdapter messageAdapter;
    private MessagePresenter messagePresenter;
    public int pageIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_message);

        recyclerView = findViewById(R.id.recyclerView);
        shSwipeRefreshLayout = findViewById(R.id.mShswipeRefreshLayout);

        initData();
        initRefresh();
        setTitle(true,"我的消息");
    }

    public void initData() {
        messagePresenter = new MessagePresenter(this);
        initRecycler();
    }

    public void initRecycler() {
        list = new ArrayList<>();
        messageAdapter = new MessageAdapter(list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(messageAdapter);
        messagePresenter.getMessage(1);
        messageAdapter.setOnItemClickListener(new MessageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(MessageActivity.this, MessageShowActivity.class);
                intent.putExtra("MessageBean", list.get(position));
                startActivity(intent);
            }
        });
        messageAdapter.setItemLongClick(new MessageAdapter.ItemLongClick() {
            @Override
            public void ItemLongClick(View view, int position) {
                showPopMenu(view, position);
            }
        });
    }

    private void initRefresh(){
        shSwipeRefreshLayout.setOnRefreshListener(new SHSwipeRefreshLayout.SHSOnRefreshListener() {
            @Override
            public void onRefresh() {
                messagePresenter.getMessage(1);
            }

            @Override
            public void onLoading() {
                messagePresenter.getMessage(pageIndex + 1);
            }

            @Override
            public void onRefreshPulStateChange(float v, int i) {

            }

            @Override
            public void onLoadmorePullStateChange(float v, int i) {

            }
        });
    }

    public List<MessageBean> getList() {
        return list;
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public void showPopMenu(View view, final int pos) {
        PopupMenu popupMenu = new PopupMenu(this, view);
        popupMenu.getMenuInflater().inflate(R.menu.menu_item, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                messagePresenter.deleteMessage(list.get(pos).getId(), pos);
                return false;
            }
        });
        popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
            @Override
            public void onDismiss(PopupMenu menu) {
            }
        });
        popupMenu.show();
    }

}
